package it.stessaro.lostcities.core;
import it.stessaro.lostcities.ai.MoveEngine;
import it.stessaro.lostcities.collection.Card;
import it.stessaro.lostcities.collection.DiscardPiles;
import it.stessaro.lostcities.collection.ExpeditionPiles;
import it.stessaro.lostcities.collection.pile.Deck;
import it.stessaro.lostcities.collection.pile.Hand;

import java.util.Observable;


public class Model extends Observable{
	
    private Deck mazzo;
	private Hand player_hand, pc_hand;
	private ExpeditionPiles playerExpeditions;
	private ExpeditionPiles pcExpeditions;
	private DiscardPiles discardPile;
	private MoveEngine pcEngine;
	private boolean playerTurn = true;
	public boolean gameRunning = true;
	//

	public Model(){
		mazzo = new Deck();
		player_hand = mazzo.getPlayerHand();
		pc_hand = mazzo.getHandPC();
		//
		discardPile = new DiscardPiles();
		//
		playerExpeditions = new ExpeditionPiles();
		pcExpeditions = new ExpeditionPiles();
		//
		pcEngine = new MoveEngine(pcExpeditions);
	}
	
	public DiscardPiles getDiscardPile() {
		return discardPile;
	}

	public boolean isPlayerTurn() {
		return playerTurn;
	}

	public void doPCMove() {
		//new solution
		if (gameRunning) {
			pc_hand.print();
			//Order the best color to play
			Card pcCardChoose = pcEngine.updateHand(pc_hand, pcExpeditions, discardPile, playerExpeditions);
			
			if (pcExpeditions.addCard(pcCardChoose)) {//TODO expedition playcard
				Log.print("addcard ok");
				pc_hand.remove(pcCardChoose);
				this.setChanged();
				this.notifyObservers();
			}else{
				Log.print("addcard ko");
			}
		}
		this.pcDrawCard();
		//valid.print();
	}

	
	public Hand getPlayerHand(){
		return player_hand;
	}
	
	public Deck getDeck(){
		return mazzo;
	}
	
	public String getDeckSize(){
		return mazzo.sizeStr();
	}

	public void playerDrawCard(){
		mazzo.playerDrawCard();
		if (mazzo.size() == 0) {
			gameRunning = false;
			this.setChanged();
			this.notifyObservers();
		}
	}
	
	public void pcDrawCard(){
		mazzo.pcDrawCard();
		if (mazzo.size() == 0) {
			gameRunning = false;
			this.setChanged();
			this.notifyObservers();
		}
	}
	
	/**/
	public ExpeditionPiles getExpeditions(){
		return playerExpeditions;
	}
	public ExpeditionPiles getPcExpeditions(){
		return pcExpeditions;
	}
	//TODO change type?
	/*Add a card to player expedition*/
	public void addCardToExpedition(Card card){
		playerExpeditions.addCard(card);
	}
	/*Add a card to pc expedition*/
	public void addCardToPcExpedition(Card card){
		pcExpeditions.addCard(card);
	}
	/*Add a card to discard*/
	public void addCardToDiscard(Card card){
		discardPile.addCard(card);
	}

	public Card drawCard(String colore){
		return discardPile.drawCard(colore);
	}
	/**
	 * @return the pc_hand
	 */
	public Hand getPc_hand() {
		return pc_hand;
	}
}
